package com.ssm.controller;

import com.ssm.pojo.User;
import com.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("user.html")
    public String user(){
        return "user/login";
    }

    @RequestMapping("add.html")
    public String add(User user){
        userService.addUser(user);
        return "user/login";
    }

    @RequestMapping("get.html")
    public String get(String name, String password , Model model, HttpSession session){
        User user = userService.getUserById(name);

        if (user == null || !user.getPassword().equals(password)) {
            //用户名或密码错误
            model.addAttribute("error", "用户名或密码错误");
            return "user/login";
        }
        session.setAttribute("SESSION_USER", user);
        return  "forward:getLise.html";
    }

    @RequestMapping("logout.html")
    public String logout(HttpSession session) {
        session.invalidate();
        return "/user/login";
    }

}
